<?php

namespace plugin\admin\app\controller;

use app\model\mongo\AgreementMongo;
use plugin\admin\app\model\RlAgreementModel;
use support\Request;
use support\Response;

/**
 * 协议列表 
 */
class RlAgreementController extends Base
{

    protected $model = null;
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        return view('rl-agreement/index');
    }

    /**
     * 获取
     * @return Response
     */
    public function select(Request $request): Response
    {
        $page = (int)$request->get('page');
        $limit = (int)$request->get('limit', 10);
        $where['status']=1;
        if ($id=$request->get('id','')){
            $where['id']=$id;
        }
        $count=RlAgreementModel::where($where)->count();
        $offset=($page-1)*$limit;
        $lists= RlAgreementModel::where($where)->orderBy('id','desc')
            ->offset($offset)->limit($limit)->get()->toArray();
        return json(['code' => 0, 'data' => $lists, 'count'=>$count,'msg' => '成功']);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $insertData=$request->only(['title','code','content']);
            $insertData['status']=1;
            $ret= RlAgreementModel::insert($insertData);
            if (!$ret){
                return $this->fail('添加协议失败');
            }
            return $this->success('添加协议成功');
        }
        return view('rl-agreement/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $updateData=$request->only(['title','code','content']);
            $id=$request->post('id','');
            $ret=  RlAgreementModel::updateOrInsert([
                'id'=>$id
            ],$updateData);
            if (!$ret){
                return $this->fail('更新协议失败');
            }
            return $this->success('更新协议成功');
        }
        return view('rl-agreement/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        $id=$request->post('id','');
        $ret=  RlAgreementModel::where('id',$id)->update([
            'status'=>2
        ]);
        if (!$ret){
            return $this->fail('删除协议失败');
        }
        return $this->success('删除协议成功');
    }

}
